package com.googlecode.pwlodars.piteststat.filesystembridge;

import static com.google.common.io.CharStreams.newReaderSupplier;
import static com.google.common.io.Resources.getResource;
import static com.google.common.io.Resources.newInputStreamSupplier;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;

public class TextResourcesConnector {

	public String readTextResourceContent(String resourceName) {
		
		InputSupplier<InputStream> contentSupplier = newInputStreamSupplier(getResource(resourceName));
		return readContent(contentSupplier);
	}
	
	public String readTextFromFile(File file){
		InputSupplier<? extends InputStream> contentSupplier =Files.newInputStreamSupplier(file);
		return readContent(contentSupplier);
	}
	
	public String readTextFromFile(String filePath){
		return readTextFromFile(new File(filePath));
	}

	private String readContent(InputSupplier<? extends InputStream> contentSupplier) {
		try {
			return CharStreams.toString(newReaderSupplier(contentSupplier, Charsets.UTF_8));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
